package com.xncoding.elasticsearch.rest;

import com.xncoding.elasticsearch.entity.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ESRestTemplateController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;

    /**
     * 创建索引
     * Javabean
     * @return
     */
    @RequestMapping("/cretatBeanIndex")
    public boolean CretatBeanIndex(){
        boolean index = false;
        // 创建索引，会根据Item类的@Document注解信息来创建
        index = elasticsearchTemplate.createIndex(Item.class);

        return index;
    }

    /**
     * 创建索引
     * 传入字符串
     * @return
     */
    @RequestMapping("/cretatStrIndex/{esindex}")
    public boolean CretatStrIndex(@PathVariable("esindex") String esindex){
        boolean index = false;
        index = elasticsearchTemplate.createIndex(esindex);
        return index;
    }

    /**
     * 创建引用
     * @return
     */
    @RequestMapping("/putBeanMapping")
    public boolean putBeanMapping(){
        boolean index = false;
        // 配置映射，会根据Item类中的@Id、@Field等字段来自动完成映射
        index = elasticsearchTemplate.putMapping(Item.class);

        return index;
    }

    /**
     * 判斷是否存在索引
     * @return
     */
    @RequestMapping("/indexExists/{esindex}")
    public boolean indexExists(@PathVariable("esindex") String esindex){
        boolean index = false;
        index = elasticsearchTemplate.indexExists(esindex);

        return index;
    }

    /**
     * 删除索引
     */
    @RequestMapping("/deleteIndex/{esindex}")
    public boolean deleteIndex(@PathVariable("esindex") String esindex){
        boolean result = elasticsearchTemplate.deleteIndex(esindex);
        return result;
    }


}
